package com.borqsprocessmanager.info;

import java.util.LinkedList;

import com.borqsprocessmanager.App;
import com.borqsprocessmanager.util.FileUtil;
import com.borqsprocessmanager.util.Log;
import com.borqsprocessmanager.util.MathUtil;
import com.borqsprocessmanager.util.PreferencesUtil;

public abstract class BaseBatteryAlsInfo {

    protected String TAG = "BaseBatteryAlsInfo";
    private final int ARG_COUNT = 3;

    public int type = 1000;
    public int eorrCount = 0;

    private LinkedList<Float> argTimeList;

    private FileUtil mFileUtil;

    // private LinkedList<Float> argTimeList;

    private String sharTimeConutKey = "";
    private String sharfreKey = "";
    private String fileName = "";

    private int totalTimeCount = 0;
    private float totalTimeM = 0;

    // private double argTimeM = 0;

    protected abstract String getSharfreKey();

    protected abstract String getSharTimeConutKey();

    protected abstract String getFileName();

    public BaseBatteryAlsInfo(int type) {
        this.type = type;
        init();
    }

    // public BaseBatteryAlsInfo() {
    // init();
    // }

    public void init() {
        Log.v(TAG, "init");
        // 从shar中取出 argtimeM ,timeCount
        sharTimeConutKey = getSharTimeConutKey();
        sharfreKey = getSharfreKey();
        fileName = getFileName();

        totalTimeM = PreferencesUtil.getInstance(App.mContext).getFloat(
                sharfreKey, 0);
        totalTimeCount = PreferencesUtil.getInstance(App.mContext).getInt(
                sharTimeConutKey, 0);

        argTimeList = getArgTimeList();

        Log.v(TAG, "allTimeM===" + totalTimeM);
        Log.v(TAG, "BatteryAnalysisInfo===" + totalTimeCount);
    }

    public Float getArgTimeM() {
        Log.v(TAG, "getArgTimeM");

        if (totalTimeCount > ARG_COUNT) {
            Log.v(TAG, "argTimeList.size()" + totalTimeCount);
            float argTime = (float) MathUtil.divide(totalTimeM, totalTimeCount,
                    2);
            Log.v(TAG, "argTime=" + argTime);
            return argTime;
        }
        return (float) 0;
    }

    /**
     * 计算平均时间
     */
    public void setArgTimeM(float timeM) {
        Log.v(TAG, "setArgTimeM");
        if (totalTimeCount >= 60) {
            Log.v(TAG, "setArgTimeM_argTimeList.size==100_" + totalTimeCount);
            Log.v(TAG, "setArgTimeM_argTimeList.size==100_allTimeM"
                    + totalTimeM);
            totalTimeM -= argTimeList.get(0);
            argTimeList.remove(0);
            totalTimeCount--;
            Log.v(TAG, "setArgTimeM_argTimeList.size==100_allTimeMed"
                    + totalTimeM);
        }
        argTimeList.add(timeM);
        totalTimeCount++;
        totalTimeM += timeM;

        if (totalTimeCount % 2 == 0) {
            Log.v(TAG, "argTimeList.size() % 3==0___" + argTimeList.size());
            saveArgTimeMToFile();
        }
        Log.v(TAG, "argTimeList.size" + argTimeList.size());
        Log.v(TAG, "allTimeMed" + totalTimeM);
    }

    /**
     * 把平均时间倍数和个数存入shar
     */
    public void saveArgTimeMToFile() {

        PreferencesUtil.getInstance(App.mContext).putFloat(sharfreKey,
                totalTimeM);
        PreferencesUtil.getInstance(App.mContext).putInt(sharTimeConutKey,
                totalTimeCount);
        if (argTimeList != null) {
            mFileUtil.writeObject(argTimeList, fileName);
        }
    }

    public void initArgTime() {
        Log.v(TAG, "initArgTime");
        if (argTimeList == null) {
            argTimeList = getArgTimeList();
        }
        argTimeList.clear();
        totalTimeCount = 0;
        totalTimeM = 0;
        saveArgTimeMToFile();
    }

    private LinkedList<Float> getArgTimeList() {
        mFileUtil = new FileUtil();
        LinkedList<Float> timeList = mFileUtil.readObject(fileName);
        if (timeList == null) {
            timeList = new LinkedList<Float>();
        }
        return timeList;
    }

}
